﻿using System;
using System.Net;
using System.Net.Http;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using API;
using static Android.Media.MediaPlayer;

namespace mb.Droid
{

    // public class MyMediaDataSource : MediaDataSource
    // {
    //     static ILog logger = ServiceLocator.Instance.Get<ILog>();

    //     IDataStream stream;
    //     string url;
    //     string key;
    //     byte[] keyBuffer;

    //     long readMaxPosition;

    //     public MyMediaDataSource(string url, string key)
    //     {
    //         var cdsMgr = ServiceLocator.Instance.Get<ICacheDataStreamManager>();
    //         this.stream = cdsMgr.CreateDataStream(url);

    //         this.readMaxPosition = 0;
    //         this.url = url;
    //         this.key = key;
    //         if (key != null)
    //             this.keyBuffer = System.Text.Encoding.ASCII.GetBytes(key);

    //         var tid = System.Threading.Thread.CurrentThread.ManagedThreadId;
    //         logger.Log($"{tid}: media create thread", this.key);
    //     }

    //     public override long Size => stream.GetLength();
    //     public override void Close()
    //     {
    //         logger.Log("MyMediaDataSource close", this.url);
    //         stream.Close();
    //     }
    //     public override int ReadAt(long position, byte[] buffer, int offset, int size)
    //     {
    //         var tid = System.Threading.Thread.CurrentThread.ManagedThreadId;
    //         // logger.Log($"{tid}: media read  thread", position, size);

    //         readMaxPosition = Math.Max(readMaxPosition, position + size);

    //         if ((keyBuffer?.Length).GetValueOrDefault() == 0)
    //         {
    //             return stream.Read(position, buffer, offset, size);
    //         }
    //         else
    //         {
    //             int read = stream.Read(position, buffer, offset, size);

    //             long koff = position;
    //             for (int i = 0; i < read; i++, koff++)
    //                 buffer[i] = (byte) (buffer[i] ^ keyBuffer[koff % keyBuffer.Length]);

    //             return read;
    //         }
    //     }

    //     public long GetAvailableLength(long from) => stream.GetAvailableLength(from);
    // }
}